package com.top.mall.main.mine

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.CouponScore
import com.top.mall.jetpack.vm.AccessViewModel
import com.top.mall.jetpack.vm.CouponViewModel
import kotlinx.android.synthetic.main.include_integral_change.*

class IntegralChangeFragment : OwnerViewModeFragment<CouponViewModel>() {

    companion object {
        fun newInstance(): IntegralChangeFragment {
            return IntegralChangeFragment()
        }
    }

    private val exchangeAdapter =
        object : BaseQuickAdapter<CouponScore, BaseViewHolder>(R.layout.item_integral_change) {
            override fun convert(holder: BaseViewHolder, item: CouponScore) {
                holder.setText(R.id.tvIntegralChangePrice,"${item.money}")
                    .setText(R.id.tvIntegralChangeCount,"${item.score}积分兑换")
                holder.getView<View>(R.id.divider).visibility =
                    if (holder.layoutPosition == data.size - 1) View.INVISIBLE else View.VISIBLE
            }
        }

    override val layoutResId: Int
        get() = R.layout.fragment_integral_change

    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override fun initView() {
        rvIntegralChangeBody.adapter = exchangeAdapter.apply {
            setOnItemClickListener { _, view, i ->
                val itemData = getItem(i)
                viewModel.userCouponExchange(itemData.money)
            }
        }
    }

    override fun initData() {

        viewModel.userCouponScore()
    }

    override fun initViewMode() {
        viewModel.userCouponScoreLiveData.observe(this as LifecycleOwner,{response ->
            if (response.status != Status.SUCCESS){
                return@observe
            }
            val data = response.data?.data
                ?:return@observe
            exchangeAdapter.setNewInstance(data)
        })
        viewModel.userCouponExchangeLiveData.observe(this as LifecycleOwner,{response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS){
                return@observe
            }
            toast(response.data?.msg)
        })

    }

}